//
//  PindaoTableViewController.swift
//  doubandiantai
//
//  Created by 王海申 on 2017/8/1.
//  Copyright © 2017年 王海申. All rights reserved.
//

import UIKit
import SwiftyJSON


protocol ChannelProtocol {
    func onChangeChanel(channel_id:String)
}

class PindaoTableViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

    @IBOutlet weak var tv: UITableView!
    
    var delegate:ChannelProtocol?
    
    public var preController:UIViewController?
    
    var channelData:[JSON] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tv.delegate = self;
        tv.dataSource = self;
        
        view.alpha = 0.8

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */
    
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       
        return channelData.count;
        
    }
    
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "pindao", for:indexPath)
        
        
        let rowData:JSON = self.channelData[indexPath.row]
        
        cell.textLabel?.text = rowData["name"].string
        
        return cell;
    }
    
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        
       
        
        
        
        
        delegate?.onChangeChanel(channel_id: self.channelData[indexPath.row]["channel_id"].string!)
        
         self.dismiss(animated: true, completion:nil)
    
    }
    
    
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        
        
        
        if segue.identifier == "myview"{
            
            let controller = segue.destination as! MyViewController
            
            controller.onChangeChanel(channel_id: sender as! String)
            
        }
        
        
        
    }
    
    

}
